package com.yanqu.road.server.cmd.activity.lanruosi;

import com.yanqu.road.entity.activity.lanruosi.LanRuoSiConfig;
import com.yanqu.road.entity.activity.lanruosi.LanRuoSiUserData;
import com.yanqu.road.pb.activity.LanRuoSiProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.lanruosi.LanRuoSiModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.lanruosi.LanRuoSiMgr;
import com.yanqu.road.server.pb.LanRuoSiPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_LANRUOSI_ELIMINATE_BLOCK, desc = "消除方块")
public class LanRuoSiEliminateBlockCmd extends AbstractCommand {

    private int multi;
    private int fromX;
    private int fromY;
    private int toX;
    private int toY;

    private LanRuoSiUserData userData = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        LanRuoSiProto.LanRuoSiEliminateReqMsg reqMsg = LanRuoSiProto.LanRuoSiEliminateReqMsg.parseFrom(packet.getBytes());
        multi = reqMsg.getMulti();
        fromX = reqMsg.getFromX();
        fromY = reqMsg.getFromY();
        toX = reqMsg.getToX();
        toY = reqMsg.getToY();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        LanRuoSiConfig config = LanRuoSiMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_OPEN;
            return;
        }

        // 旧配置的活动不能用16倍
        if (multi == 16 && config.getFastUnlockVipLevel() == Integer.MAX_VALUE) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK_MULTI;
            return;
        }

        LanRuoSiModule module = player.getModule(LanRuoSiModule.class);
        userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK;
            return;
        }

        if (multi != 1 && multi != 8 && multi != 16) {
            ret = GameErrorCode.E_LANRUOSI_ACTIVITY_EXCHANGE_PARAM_ERROR;
            return;
        }

        int vipLv = VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp());
        if (multi == 8) {
            if (userData.getScore() < config.getMultiUnlockScore()
                    && vipLv < config.getMultiUnlockVipLevel()) {
                ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK_MULTI;
                return;
            }
        } else if (multi == 16) {
            if (vipLv < config.getFastUnlockVipLevel()) {
                ret = GameErrorCode.E_LANRUOSI_ACTIVITY_NO_UNLOCK_MULTI;
                return;
            }
        }

        ret = module.eliminateBlock(fromX, fromY, toX, toY, multi);

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            LanRuoSiProto.LanRuoSiEliminateRespMsg.Builder builder = LanRuoSiProto.LanRuoSiEliminateRespMsg.newBuilder();
            builder.setRet(ret);
            if (userData != null) {
                builder.setGameData(LanRuoSiPb.parseGameDataMsg(userData, multi));
            }
            player.sendPacket(Protocol.U_LANRUOSI_ELIMINATE_BLOCK, builder);
        }
    }
}
